package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.Exception.VisiableException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Controller增强类的标签
 * 在@Controller注解的类的映射（@RequestMapping）方法前后进行功能增强
 *
 * 之前：对请求参数进行一些处理
 * 之后：对请求方法之后进行一些操作
 *
 *     请求方法处理过程中进行处理
 *
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(VisiableException.class)
    public void displayExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(e.getMessage())));
    }

    @ExceptionHandler(RuntimeException.class)
    public void runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult("系统出现未知异常，请联系客服。")));
    }
}
